﻿package com.tjsinfo.tjsoa.export.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.util.MessageResources;
import org.dom4j.Document;
import org.json.JSONArray;
import org.json.JSONObject;

import bsh.This;

import com.tjsinfo.tjsoa.articles.vo.TIndexArticles;
import com.tjsinfo.tjsoa.common.FileManage;
import com.tjsinfo.tjsoa.common.PaginationSupport;
import com.tjsinfo.tjsoa.common.SessionUser;
import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.documentationflow.dangweifawen.vo.TDangweifawenNeirong;
import com.tjsinfo.tjsoa.documentationflow.xingzhengfawen.vo.FaWenInfo;
import com.tjsinfo.tjsoa.documentationflow.xingzhengfawen.vo.TXingzhengfawenNeirong;
import com.tjsinfo.tjsoa.export.bo.IExportManage;
import com.tjsinfo.tjsoa.meetingroom.action.newArrangementsAction;
import com.tjsinfo.tjsoa.system.vo.User;


import javax.servlet.http.HttpSession;

public class ExportManageAction extends DispatchAction {
	
	private IExportManage exportManage;

	public IExportManage getExportManage() {
		return exportManage;
	}

	public void setExportManage(IExportManage exportManage) {
		this.exportManage = exportManage;
	}

/**
 * 前台获取最近文章列表
 * 按月份查询
 */
	public ActionForward getArticlesList(ActionMapping mapping, ActionForm form,
		HttpServletRequest request, HttpServletResponse response)
		throws Exception {
		
		try {
				
			    String cidsString = ToolKit.getProperties("com/tjsinfo/tjsoa/export/properties/messageResource.properties", this).getProperty("cids"); 
			    Map <Integer,String> cidnames = exportManage.findExportCidNames(cidsString);
				String strStart=request.getParameter("startdate");
				
				List<TIndexArticles> articlelistpage = findArticles(cidsString,strStart);
				
				//将分页对象中获得object列表转换成Articles列表
			
		 		// 输出查到的文章列表数据	
		 		JSONArray array	 = new JSONArray();
		 			for (TIndexArticles articlesL : articlelistpage) {
		 				JSONObject object = new JSONObject();
		 				object.put("uuid", articlesL.getUuid());
		 				object.put("title", articlesL.getTitle());
		 				object.put("id", articlesL.getId());
		 				object.put("createDate", articlesL.getCreateDate());
		 				object.put("cid", cidnames.get(articlesL.getCid()));
		 				array.put(object);
		 			}
			 		JSONObject jsonObject = new JSONObject();  
			 		jsonObject.put("aricleslist", array);
			 		response.getWriter().print(jsonObject);

	 	   	} catch (IOException e) {
		 			// TODO Auto-generated catch block
		 			e.printStackTrace();
 		} 
		 		return null;
	}

	private List<TIndexArticles> findArticles(String cidsString, String strStart) {
	// TODO Auto-generated method stub
		Date startDate = new Date();
		if (strStart!=null){
			startDate=ToolKit.FormatDate(strStart+"-01");
 		}
		Date endDate = addmonth(startDate);
 		PaginationSupport ps;
		ps = exportManage.findByCids(cidsString,"0", "1000", startDate, endDate);
 		List<TIndexArticles> articlelistpage = ps.getItems();
	return articlelistpage;
}

	private Date addmonth(Date startDate) {
	// TODO Auto-generated method stub
		Calendar calendar =Calendar.getInstance();
		calendar.setTime(startDate);
		calendar.add(Calendar.MONTH,1);
	return calendar.getTime();
}

	/**
	 * 导出选中的文章
	 */
	public ActionForward exportArticles(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
					String stryueString=request.getParameter("startdate");
					String cidsString = ToolKit.getProperties("com/tjsinfo/tjsoa/export/properties/messageResource.properties", this).getProperty("cids"); 

						List<TIndexArticles> articlesList = findArticles(cidsString, stryueString);
						
						String fromFilePathString = ToolKit.getProperties("com/tjsinfo/tjsoa/articles/properties/messageResource_zh_CN.properties", this).getProperty("filepath");
						String toFilePathString = ToolKit.getProperties("com/tjsinfo/tjsoa/export/properties/messageResource.properties", this).getProperty("tempfilepath"); 
						String fujianPathString = ToolKit.getProperties("com/tjsinfo/tjsoa/export/properties/messageResource.properties", this).getProperty("rootpath"); 

						String  useruuid = ToolKit.getSessionUser(request).getUUID();
						toFilePathString = toFilePathString +"\\"+ useruuid;
						exportManage.mkdir(toFilePathString);
						exportManage.createArticlesXML(articlesList,toFilePathString);
						for (TIndexArticles articlesL : articlesList) {
							exportManage.createArticlesHtml(articlesL,fromFilePathString,toFilePathString,fujianPathString);
						}
						String zipFileName = exportManage.pack(toFilePathString,fujianPathString);
						// 输出压缩文件名	
						JSONObject jsonObject = new JSONObject(); 
						jsonObject.put("fileName", zipFileName);   //当前页文章列表
						response.getWriter().print(jsonObject);
			 		return null;
		}

	/**
	 * 前台获取最近文件列表
	 * 按月份查询
	 */
		public ActionForward getDocList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
			
			try {
					
				    String strStart=request.getParameter("startdate");
					Date startDate = new Date();
					if (strStart!=null){
						startDate=ToolKit.FormatDate(strStart+"-01");
			 		}
					Date endDate = addmonth(startDate);
			 		List<FaWenInfo> faWenInfoList = exportManage.selectWenJian(SessionUser.getUserId(request), 0, 1000, startDate, endDate);

				
			 		// 输出查到的文章列表数据	
			 		JSONArray array	 = new JSONArray();
			 			for (FaWenInfo docInfo : faWenInfoList) {
			 				JSONObject object = new JSONObject();
			 				object.put("id", docInfo.getId());
			 				object.put("title", docInfo.getBiaoti());
			 				object.put("wenyear", docInfo.getWenyear());
			 				object.put("wenhaotype", docInfo.getWenhaotype());
			 				object.put("fawendatetime", docInfo.getFawendatetime());
			 				object.put("wentype", docInfo.getWentype());
			 				object.put("tablename", docInfo.getTableName());
			 				array.put(object);
			 			}
				 		JSONObject jsonObject = new JSONObject();  
				 		jsonObject.put("doclist", array);   //当前页文件列表
			 			response.getWriter().print(jsonObject);

		 	   	} catch (IOException e) {
			 			// TODO Auto-generated catch block
			 			e.printStackTrace();
	 		} 
			 		return null;
		}
		
		/**
		 * 导出选中的文件
		 */
		public ActionForward exportDoc(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response)
				throws Exception {
					String strStart=request.getParameter("startdate");
					Date startDate = new Date();
					if (strStart!=null){
						startDate=ToolKit.FormatDate(strStart+"-01");
			 		}
					Date endDate = addmonth(startDate);
			 		List<FaWenInfo> faWenInfoList = exportManage.selectWenJian(SessionUser.getUserId(request), 0, 1000, startDate, endDate);

					String toFilePathString = ToolKit.getProperties("com/tjsinfo/tjsoa/export/properties/messageResource.properties", this).getProperty("tempfilepath"); 
					String rootPathString = ToolKit.getProperties("com/tjsinfo/tjsoa/export/properties/messageResource.properties", this).getProperty("rootpath"); 
					String  useruuid = ToolKit.getSessionUser(request).getUUID();
					toFilePathString = toFilePathString +"\\"+ useruuid;
					exportManage.mkdir(toFilePathString);
					Document xmlDocument=exportManage.createDocsXML(toFilePathString);
						
					for(FaWenInfo faweninfo : faWenInfoList){
						if("xingzhengfawen".equals(faweninfo.getTableName())){
							TXingzhengfawenNeirong docxztemp = exportManage.findXZDocById(faweninfo.getId());
							xmlDocument = exportManage.copyDoc(docxztemp,toFilePathString,rootPathString,xmlDocument);
						}else{
							TDangweifawenNeirong docdwtemp = exportManage.findDWDocById(faweninfo.getId());
							xmlDocument=exportManage.copyDoc(docdwtemp,toFilePathString,rootPathString,xmlDocument);
						}
					}
						exportManage.writeDocsXML(xmlDocument,toFilePathString);
						String zipFileName = exportManage.pack(toFilePathString,rootPathString);
						// 输出压缩文件名	
						JSONObject jsonObject = new JSONObject(); 
						jsonObject.put("fileName", zipFileName);   //当前页文章列表
						response.getWriter().print(jsonObject);
				 		return null;
			}
	 		 
}
